package logic

import (
	"context"
	"furni-set/models/cache"
	"furni-set/models/mysql"
	"furni-set/setrpc/internal/svc"
	"furni-set/setrpc/setrpc"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
	"k8s.io/apimachinery/pkg/util/json"
)

type GoodsListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsListLogic {
	return &GoodsListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// goods的列表方法
func (l *GoodsListLogic) GoodsList(in *setrpc.GoodsListRequest) (*setrpc.GoodsListResponse, error) {
	// todo: add your logic here and delete this line
	var goodLists []*setrpc.GoodsList

	result, _ := cache.Rdb.Get(l.ctx, "goodsList").Result()
	if result == "" {
		goodLists = mysql.GoodsList(mysql.DB, goodLists, in)
		marshal, _ := json.Marshal(goodLists)
		cache.Rdb.Set(l.ctx, "goodsList", marshal, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &goodLists)
	}

	return &setrpc.GoodsListResponse{
		List: goodLists,
	}, nil
}
